package pdm.yamba;

import java.io.InputStream;
  
import pdm.yamba.dto.User;
import pdm.yamba.service.IUserInfoPullService;
import pdm.yamba.service.UserInfoPullService;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class UserInfoActivity extends Activity {

    private MainApplication application;    
    private IUserInfoPullService _remoteBinder;
    private Intent _bindIntent;    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        
//        application = (MainApplication) getApplication();
//        User user = application.getTwitterFeed().GetUserInfo(application.getUsername());
        

        _bindIntent = new Intent(UserInfoActivity.this, UserInfoPullService.class);
        
        final ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service)
            {
                Toast.makeText(UserInfoActivity.this, "onServiceConnected()", Toast.LENGTH_LONG).show();
                _remoteBinder = IUserInfoPullService.Stub.asInterface(service);
                
                try
                {
                    User user = _remoteBinder.GetUserInfo(application.getUsername(), application.getPassword(), application.getServiceUri());
                    
                    TextView userInfo_userName = (TextView) findViewById(R.id.userInfo_userName);
                    userInfo_userName.setText(user.getUsername());
                    
                    TextView userInfo_totalMessageNumber = (TextView) findViewById(R.id.userInfo_totalMessageNumber);
                    userInfo_totalMessageNumber.setText(String.valueOf(user.getMessageCount()));
                    
                    TextView userInfo_subscriptionNumber = (TextView) findViewById(R.id.userInfo_subscriptionNumber);
                    userInfo_subscriptionNumber.setText(String.valueOf(user.getSubscriptionNumber()));
                    
                    TextView userInfo_subscribersNumber = (TextView) findViewById(R.id.userInfo_subscribersNumber);
                    userInfo_subscribersNumber.setText(String.valueOf(user.getSubscribersCount()));
                    
/*                    ImageView image = (ImageView) findViewById(R.id.ImageView);
                    try
                    {
                        image.setImageBitmap(BitmapFactory.decodeStream((InputStream)user.profileImageUrl.toURL().getContent()));
                    }
                    catch(Exception ex)
                    {}                    
*/                    
                }
                catch (RemoteException e)
                {
                }
                
            }

            @Override
            public void onServiceDisconnected(ComponentName name)
            {
                Toast.makeText(UserInfoActivity.this, "onServiceDisconnected()", Toast.LENGTH_LONG).show();
                _remoteBinder = null;
            }
        };           
    
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.user_info, menu);
        return true;
    }

}
